﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SoftwareConsulting.BI11.Studio.Core.Users;
using SoftwareConsulting.BI11.Studio.InternalServices;
using SoftwareConsulting.BI11.Studio.UI.Users.Properties;

[assembly:CLSCompliant(true)]
namespace SoftwareConsulting.BI11.Studio.UI.Users
{
    /// <summary>
    /// Класс диалога смены пароля пользователя
    /// </summary>
    public partial class UserPasswordChangeDialog : Form
    {
        private IUserInfo _user;
        private bool _cancelEnabled;

        /// <summary>
        /// Инициализация диалога
        /// </summary>
        private void Initialize()
        {
            CancelPCButton.Enabled = _cancelEnabled;
            LoginBox.Text = _user.LogOn;
            FIOBox.Text = _user.LFP;
        }

        public UserPasswordChangeDialog(bool cancelEnabled, IUserInfo user)
        {
            _user = user;
            _cancelEnabled = cancelEnabled;            
            InitializeComponent();
        }

        /// <summary>
        /// Смена пароля и закрытие диалога
        /// </summary>
        private void ApplyAndClose()
        {
            string newPassword = PasswordBox.Text;
            string newPasswordAgain = PasswordAgainBox.Text;
            if (newPassword != newPasswordAgain)
            {
                GeneralMethods.ShowUserError(Resources.BI_USERPASSWORDCHANGE_AGAINWRONG);
                PasswordAgainBox.Focus();
                PasswordAgainBox.SelectAll();
                DialogResult = DialogResult.None;
                return;
            }
            int c = newPassword.Length;            
            for (int i = 0; i < c; i++)
            {
                if (((!char.IsLetterOrDigit(newPassword, i)) && (newPassword[i] != '_')) || (c < 6))
                {
                    GeneralMethods.ShowUserError(Resources.BI_USERPASSWORDCHANGE_WRONGPASSWORD);
                    PasswordBox.Focus();
                    PasswordBox.SelectAll();
                    DialogResult = DialogResult.None;
                    return;
                }
            }
            _user.Password = newPassword;
            DialogResult = DialogResult.OK;
        }

        private void UserPasswordChangeDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            if ((DialogResult != DialogResult.OK) && (!_cancelEnabled))
            {
                GeneralMethods.ShowUserError(UsersSharedMethods.FirstLogOnInformation);
                e.Cancel = true;
            }
        }        

        private void UserPasswordChangeDialog_Load(object sender, EventArgs e)
        {
            Initialize();
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            ApplyAndClose();
        }
    }
}
